//
//  XDynamicBookkeepingTableViewCell.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/9/9.
//

#import "XDynamicBookkeepingTableViewCell.h"
#import "XBookkeepingItemView.h"

@interface XDynamicBookkeepingTableViewCell ()

@property(nonatomic,weak)UIImageView* typeImage;

@property(nonatomic,weak)UILabel* typeLb;

@property(nonatomic,weak)UILabel* amtLb;

@property(nonatomic,weak)UILabel* remarkLb;

@property(nonatomic,weak)UILabel* timeLb;

/// 显示内容视图
@property(nonatomic,weak) MyLinearLayout* contentLayout;

@end

@implementation XDynamicBookkeepingTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        
        self.backgroundColor = UIColor.clearColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self initSubViews];
    }
    return self;
}

- (void)initSubViews{
    
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    contentLayout.padding = UIEdgeInsetsMake(0, 16,0, 16);
    contentLayout.myMargin = 0;
    contentLayout.myHeight = MyLayoutSize.wrap;
    contentLayout.subviewHSpace = 10;
    contentLayout.cacheEstimatedRect = YES;
    [self.contentView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    UIImageView* iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"x_dynamic_bookkeeping"]];
    iconImage.myCenterY = iconImage.myLeft = 0;
    [contentLayout addSubview:iconImage];
    
    MyLinearLayout *contextLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contextLayout.backgroundColor = UIColor.whiteColor;
    contextLayout.layer.cornerRadius = PX(16);
    contextLayout.padding = UIEdgeInsetsMake(0, 16,16, 16);
    contextLayout.myMargin = 0;
    contextLayout.myHeight = MyLayoutSize.wrap;
    contextLayout.weight = 1.0;
    [contentLayout addSubview:contextLayout];
    
    MyRelativeLayout *titleLayout = [MyRelativeLayout new];
    titleLayout.myMargin = 0;
    titleLayout.myHeight = PX(40);
    titleLayout.bottomBorderline = [[MyBorderline alloc] initWithColor:COLOR_GRAY_LINE];
    [contextLayout addSubview:titleLayout];
    
    //firstline
    UIImageView* typeImage = [[UIImageView alloc] init];
    typeImage.myCenterY = 0;
    typeImage.mySize = CGSizeMake(PX(30), PX(30));
    [titleLayout addSubview:typeImage];
    _typeImage = typeImage;
    
    //费用类型
    UILabel* typeLb = [UILabel lableWithFont:FONT_FIT(15) color:COLOR_BLACK_DARK text:@"--"];
    typeLb.myCenterY = 0;
    typeLb.leftPos.equalTo(typeImage.rightPos).offset(16);
    [titleLayout addSubview:typeLb];
    _typeLb = typeLb;

    //金额
    UILabel* amtLb = [UILabel lableWithFont:NUMBERFONT_FIT(15) color:COLOR_GREEN text:@"￥0.00"];
    amtLb.myRight = 0;
    amtLb.myCenterY = 0;
    [titleLayout addSubview:amtLb];
    _amtLb = amtLb;
    
    //备注
    UILabel* remarkLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"--"];
    [contextLayout addSubview:remarkLb];
    remarkLb.myTop = 16;
    remarkLb.myHorzMargin = 0;
    _remarkLb = remarkLb;
    
    //时间
    UILabel* timeLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_QUITE text:@"--"];
    timeLb.myTop = 10;
    [contextLayout addSubview:timeLb];
    _timeLb = timeLb;
}


- (void)setDynamic:(XDynamicModel *)dynamic{
    
    NSString* imageName;
    switch (dynamic.feeType) {
        case XFeeTypeXZ:
            imageName = @"x_income_xz_n";
            break;
        case XFeeTypeHB:
            imageName = @"x_income_hb_n";
            break;
        case XFeeTypeJJ:
            imageName = @"x_income_jj_n";
            break;
        case XFeeTypeBX:
            imageName = @"x_income_bx_n";
            break;
        case XFeeTypeLC:
            imageName = @"x_income_lc_n";
            break;
        case XFeeTypeTK:
            imageName = @"x_income_tk_n";
            break;
        case XFeeTypeTZ:
            imageName = @"x_income_tz_n";
            break;
        case XFeeTypeJZ:
            imageName = @"x_income_jz_n";
            break;
        case XFeeTypeGC:
            imageName = @"x_income_gc_n";
            break;
        case XFeeTypeQT1:
            imageName = @"x_income_qt_n";
            break;
        case XFeeTypeCY:
            imageName = @"x_expend_cy_n";
            break;
        case XFeeTypeFD:
            imageName = @"x_expend_fz_n";
            break;
        case XFeeTypeGW:
            imageName = @"x_expend_gw_n";
            break;
        case XFeeTypeHZ:
            imageName = @"x_expend_hz_n";
            break;
        case XFeeTypeRY:
            imageName = @"x_expend_ry_n";
            break;
        case XFeeTypeSD:
            imageName = @"x_expend_sd_n";
            break;
        case XFeeTypeXX:
            imageName = @"x_expend_xx_n";
            break;
        case XFeeTypeYY:
            imageName = @"x_expend_lr_n";
            break;
        case XFeeTypeSM:
            imageName = @"x_expend_sm_n";
            break;
        case XFeeTypeCW:
            imageName = @"x_expend_cw_n";
            break;
        case XFeeTypeTX:
            imageName = @"x_expend_tx_n";
            break;
        case XFeeTypeJT:
            imageName = @"x_expend_jt_n";
            break;
        case XFeeTypeGY:
            imageName = @"x_expend_gy_n";
            break;
        case XFeeTypeKD:
            imageName = @"x_expend_kd_n";
            break;
        case XFeeTypeSJ:
            imageName = @"x_expend_sj_n";
            break;
        case XFeeTypeYL:
            imageName = @"x_expend_yl_n";
            break;
        case XFeeTypeQT2:
            imageName = @"x_expend_qt_n";
            break;
        default:
            break;
    }
    
    self.typeImage.image = [UIImage imageNamed:imageName];
    [self.typeImage sizeToFit];
    
    self.typeLb.text = dynamic.title;
    [self.typeLb sizeToFit];

    self.amtLb.text = [NSString stringWithFormat:@"%@ ￥%.2f",dynamic.feeType > 200?@"-":@"+",dynamic.feeAmt];
    self.amtLb.textColor = dynamic.feeType > 200?COLOR_RED:COLOR_GREEN;
    [self.amtLb sizeToFit];
    
    self.remarkLb.text = dynamic.content;
    [self.remarkLb sizeToFit];
    
    self.timeLb.text = [dynamic.date stringCutSeconds];
    [self.timeLb sizeToFit];
}

#pragma private -- Size

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority{
    
    CGSize cellSize = CGSizeZero;
    if (@available(iOS 11.0, *)) {
        cellSize = [self.contentLayout sizeThatFits:CGSizeMake(targetSize.width - self.safeAreaInsets.left - self.safeAreaInsets.right, targetSize.height)];
    } else {
        CGSize size = targetSize;
        size.width = size.width;
        cellSize = [self.contentLayout sizeThatFits:size];  //如果使用系统自带的分割线，请记得将返回的高度height+1
    }
    return cellSize;
}
@end
